class ServiceLiveVideoController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ServiceService, iRunner, CpService, getCpListCamera, getCourseList){

        var vm = this;

        vm.$rootScope     = $rootScope;
        vm.$cookieStore   = $cookieStore;
        vm.$timeout       = $timeout;
        vm.$state         = $state;
        vm.ngDialog       = ngDialog;
        vm.dialogService  = dialogService;
        vm.ServiceService = ServiceService;
        vm.iRunner        = iRunner;
        vm.CpService      = CpService
        vm.getCourseList  = getCourseList;
        vm.getCpListCamera = getCpListCamera;

        // 科目不为空, 默认选中第一个
        // vm.courseList = vm.getCourseList.data;
        // if (vm.courseList.length>0) {
        //     vm.course = vm.courseList[0];
        // }

        //播放器列表
        vm.player = {};



        // 获取123赛事id
        vm.CpService.getTrelated(vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
        .then( data => {

            if (data.code == 0) {
                vm.camera_id = data.data.related_id;
                if (vm.camera_id == 0) {
                    vm.dialogService.error("此赛事未支持视频直播功能！");
                    return;
                }


                var baseurl = "http://www.123yd.cn/xingjiansport/V2/XiaoAi/getCameraList?raceid=" + vm.camera_id;
                $.get(baseurl, {}, function (data) {
                    // console.log(data.result);

                    // 成功获取摄像列表的场合
                    if (data.errorcode == 0) {

                        // 获取数据
                        vm.videoList = data.result;
                        // console.log(vm.camera_id);
                        console.log(vm.getCpListCamera.data);
                        console.log(vm.videoList);


                        angular.forEach(vm.getCpListCamera.data, function(data, index, array) {

                            angular.forEach(vm.videoList, function(data2, index2, array2) {
                                if (data.camera_id.indexOf(data2.cameraid) != -1) {
                                    // console.log(data);
                                    // console.log(data2);
                                    // console.log('-----');
                                    data2.name = data.cp_name + '(' + data2.cameraid + ')';
                                }
                            });
                        });


                        vm.$timeout( () => { 
                            angular.forEach(vm.videoList, function(data, index, array){

                                    // 直播视频窗口
                                    vm.player[data.cameraid] = new MediaElementPlayer('#' + data.cameraid, {
                                        loop: true,
                                        clickToPlayPause: false,
                                        success: function (media, ele, player) {
                                            player.setSrc(data.url);
                                            if (index == 0) {
                                                // player.play(); 
                                            }
                                        }
                                    });
                            });
                         }, 500);

                    } else {
                        vm.dialogService.error(data.errormsg);
                    }

                }, 'json');

            }
            // 调用失败, 提示用户
            (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);
            // 登录信息已过期, 请重新登录
            data.code==-1001&&vm.dialogService.error(data.message, true);
        }, error => console.log('error'));
        
        /**
         * [searchRunnerVideo 搜索选手视频]
         * @param  {[type]} value [description]
         * @return {[type]}       [description]
         */
        vm.searchRunnerVideo = function(courseId, bib) {
            // var URL = iRunner.config.baseUrl + '/realtime/personal?race_id=' + vm.$state.params.race_id + '&search[bib]=' + bib + '&access-token=' + vm.$cookieStore.get('access_token');
            var URL = iRunner.config.baseUrl + '/realtime/personal?race_id=248&course_id=317&search[bib]=28047&access-token=' + vm.$cookieStore.get('access_token');
            // console.log(URL);

            if (!bib) {
                vm.dialogService.error("请输入选手号");
                return false;
            }

            vm.runnerVideoList = [];
            $.get(URL, {}, function (data) {
                        // console.log(data);
                        angular.forEach(data.data, function(data2, index, array) {

                            $.get('http://www.123yd.cn/xingjiansport/V2/XiaoAi/getVideoByTimeSlot?raceid=' + vm.camera_id + '&cameraid=' + data2.cp_index + '&starttime=2016-7-12 07:10:00&endtime=2016-7-12 07:10:20', {}, function (data3) {
                                vm.runnerVideoList.push({
                                    cp_name: data2.cp_name,
                                    cp_index: data2.cp_index,
                                    obj: data3
                                });
                                // console.log(vm.runnerVideoList);

                            }, 'json');
                        });

                        vm.$timeout( () => {
                            angular.forEach(vm.runnerVideoList, function(data, index, array) {
                               vm.$timeout( () => {

                                    // 直播视频窗口
                                    vm.player['runner' + data.cp_index] = new MediaElementPlayer('#runner' + data.cp_index, {
                                        loop: true,
                                        clickToPlayPause: false,
                                        success: function (media, ele, player) {
                                        }
                                    });
                                 }, 200);
                            });
                        }, 500);
                console.log();
            }, 'json');

        }

        vm.searchRunnerVideoTest = function(courseId, bib) {
            // http://121.40.140.34/xingjiansport/V2/XiaoAi/getVideoByTimeSlot?raceid=31&cameraid=17&starttime=2016-7-12 07:10:00&endtime=2016-7-12 07:10:20

            $.get('http://www.123yd.cn/xingjiansport/V2/XiaoAi/getVideoByTimeSlot?raceid=51&cameraid=34&starttime=2016-7-29 17:18:00&endtime=2016-7-29 17:19:00', {}, function (data3) {
            
                    console.log(data3);


                                vm.playertest34 = new MediaElementPlayer('#test34', {
                                    loop: true,
                                    clickToPlayPause: false,
                                    success: function (media, ele, player) {
                                            console.log("成功成功成功成功成功");
                                            console.log(data3.result.url);
                                            player.setSrc(data3.result.url);
                                            player.play(); 
                                    },
                                    error: function() {
                                        console.log("error");
                                    }
                                });
            }, 'json');

        }

    }
}

ServiceLiveVideoController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ServiceService', 'iRunner', 'CpService', 'getCpListCamera', 'getCourseList']; 

export default ServiceLiveVideoController;